﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;
using HF.Software.ClassLib;
using HF.Software.Entities;

namespace HF.Software
{
    /// <summary>
    /// hef创建于2015.02.03
    /// 系统配置文件读写
    /// </summary>
    public partial class frmSys : Form
    {
        #region event

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (null == this.oConfig) return;

            IniModelDataCollection oDataCollection = new IniModelDataCollection();
            IniWrite oIniWrite = new IniWrite();

            SYS oSys = this.oConfig.SYS;

            try
            {
                PropertyInfo[] oFields = oSys.GetType().GetProperties();

                foreach (PropertyInfo oInfo in oFields)
                {
                    IniModel oConfigIni = new IniModel();
                    oConfigIni.IN_Section = "SYS";
                    oConfigIni.IN_Key = oInfo.Name;
                    oConfigIni.IN_Value = Convert.ToString(oInfo.GetValue(oSys, null));

                    switch (oConfigIni.IN_Key.Trim())
                    {
                        case "DefalutSellKind"://默认销售类型
                            oConfigIni.IN_Value = this.cmbSaleType.SelectedValue.ToString();
                            break;
                        case "SeverPath"://数据库地址
                            oConfigIni.IN_Value = this.txtDataPath.Text.Trim();
                            break;
                        case "UpdateTime"://修改时间
                            oConfigIni.IN_Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            break;
                        case "ShowFeature"://退出是否打印交班凭证
                            if (this.chkVoucher.Checked)
                                oConfigIni.IN_Value = "T";
                            else
                                oConfigIni.IN_Value = "F";
                            break;
                        case "ShowOK"://现金准数需要二次确认
                            if (this.chkCashConfirm.Checked)
                                oConfigIni.IN_Value = "T";
                            else
                                oConfigIni.IN_Value = "F";
                            break;
                    }

                    oDataCollection.Add(oConfigIni);
                }

                foreach (IniModel oIniModel in oDataCollection)
                {
                    oIniWrite.Write(oIniModel.IN_Section, oIniModel.IN_Key, oIniModel.IN_Value);
                }
                MessageBox.Show("系统更新成功！", "提示");
            }
            catch (Exception ex)
            {
                this.oLog.fnLog("201502091412系统更新失败：" + ex.ToString());
                MessageBox.Show("系统更新失败！", "提示");
            }
        }

        #endregion

        #region create

        public frmSys()
        {
            InitializeComponent();
        }

        private void frmSys_Load(object sender, EventArgs e)
        {
            if (null == this.oConfig) return;

            List<ClsComboBoxItem> lstSaleType = new List<ClsComboBoxItem>();

            ClsComboBoxItem oCmbSaleType1 = new ClsComboBoxItem();
            oCmbSaleType1.Value = "0";
            oCmbSaleType1.Text = "0.票务";
            lstSaleType.Add(oCmbSaleType1);

            ClsComboBoxItem oCmbSaleType2 = new ClsComboBoxItem();
            oCmbSaleType2.Value = "1";
            oCmbSaleType2.Text = "1.卖品";
            lstSaleType.Add(oCmbSaleType2);

            ClsComboBoxItem oCmbSaleType3 = new ClsComboBoxItem();
            oCmbSaleType3.Value = "2";
            oCmbSaleType3.Text = "2.会员";
            lstSaleType.Add(oCmbSaleType3);

            this.cmbSaleType.DataSource = lstSaleType;
            this.cmbSaleType.ValueMember = "Value";
            this.cmbSaleType.DisplayMember = "Text";

            this.cmbSaleType.SelectedValue = this.oConfig.SYS.DefalutSellKind;

            this.txtDataPath.Text = this.oConfig.SYS.SeverPath.Trim();

            this.chkVoucher.Checked = this.oConfig.SYS.ShowFeature;

            this.chkCashConfirm.Checked = this.oConfig.SYS.ShowOK;
        }

        #endregion

        #region var

        AppConfig oConfig = new AppConfig();
        ClsLog oLog = new ClsLog(Application.StartupPath + @"\log\set\" + DateTime.Now.ToString("yyyyMMddHH") + ".txt");

        #endregion

    }
}
